var app = getApp();

Page({
  data: {
    origin: '',
    destination: '',
    air_code: '',
    timeType: '',
    startDate: '',
    endDate: '',
    sort: 'reach_date',
    time: '',
    lng: '',
    lat: '',
    p: 1,
    triplist: [],
    isPullDown: false,
    isPullUp: false,
    isLoading: false,
    isPullUpText: '',
    sortList: [{
      id: '1',
      value: 'reach_date',
      text: '按到达时间排序',
      checked: true
    },
    {
      id: '2',
      value: 'credit',
      text: '信誉优先',
      checked: false
    },
    {
      id: '3',
      value: 'reward_min',
      text: '按赏金排序',
      checked: false
    },
    {
      id: '4',
      value: 'dist',
      text: '距离',
      checked: false
    }
    ]
  },
  onLoad: function (options) {
    var that = this;
    var title = '';

    if (options.air_code != '') {
      title = '航班号：' + options.air_code;
    } else {
      if (options.origin != '' && options.destination == '') {
        title = '始发地：' + options.origin;
      } else if (options.origin == '' && options.destination != '') {
        title = '目的地：' + options.destination;
      } else if (options.origin != '' && options.destination != '') {
        title = options.origin + ' ⇀ ' + options.destination;
      }
    }

    wx.setNavigationBarTitle({
      title: title
    });

    switch (options.timeType) {
      case "1":
        that.setData({
          time: '到达时间：' + options.startDate + '—' + options.endDate
        });
        break;
      case "2":
        that.setData({
          time: '出发时间：' + options.startDate + '—' + options.endDate
        });
        break;
      case "3":
        that.setData({
          time: '出发时间：' + options.startDate + ' —到达时间：' + options.endDate
        });
        break;
    }

    that.setData({
      origin: options.origin,
      destination: options.destination,
      air_code: options.air_code,
      timeType: options.timeType,
      startDate: options.startDate,
      endDate: options.endDate
    });

    that.getList();
  },
  detailTrip: function (e) {
    var tripid = e.currentTarget.id;
    wx.navigateTo({
      url: 'tripDetail?tripId=' + tripid
    })
  },
  sortTap: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var sort = e.currentTarget.dataset.sort;
    for (var i = 0; i < that.data.sortList.length; i++) {
      that.data.sortList[i].checked = false
    }
    that.data.sortList[index].checked = true;
    if (sort == "dist") {
      wx.getLocation({
        success: function (res) {
          that.setData({
            sortList: that.data.sortList,
            sort: that.data.sortList[index].value,
            triplist: [],
            p: 1,
            lng: res.longitude,
            lat: res.latitude
          });
          that.getList();
        }
      });
    } else {
      that.setData({
        sortList: that.data.sortList,
        sort: that.data.sortList[index].value,
        triplist: [],
        p: 1
      })
      that.getList();
    }
  },
  onPullDownRefresh: function () {
    var that = this;
    if (that.data.isPullUp || that.data.isPullDown) {
      return
    }
    that.setData({
      p: 1,
      triplist: [],
      isPullDown: true,
      isPullUp: false,
      isLoading: false,
      isPullUpText: ''
    });
    that.getList();
    wx.stopPullDownRefresh();
  },
  onReachBottom: function () {
    var that = this;
    if (that.data.isPullUp || that.data.isPullDown) {
      return
    }
    that.setData({
      isPullUp: false,
      isPullDown: true,
      isLoading: true
    })
    that.getList();
  },
  getList: function () {
    var that = this;
    var jsonData = {
      origin: that.data.origin,
      destination: that.data.destination,
      air_code: that.data.air_code,
      data_type: that.data.timeType,
      depart_date: that.data.startDate,
      reach_date: that.data.endDate,
      sort: that.data.sort,
      lng: that.data.lng,
      lat: that.data.lat,
      p: that.data.p
    };
    app.JsonPost("Api/Trip/index", jsonData, function (res) {
      if (res.list.length > 0) {
        var l = that.data.triplist;
        for (var i = 0; i < res.list.length; i++) {
          res.list[i]["depart_date"] = app.formatTime(res.list[i]["depart_date"], 'M月D日');
          res.list[i]["reach_date"] = app.formatTime(res.list[i]["reach_date"], 'M月D日');
          l.push(res.list[i]);
        }
        that.setData({
          triplist: l,
          p: that.data.p + 1,
          isPullUp: false,
          isPullDown: false,
          isPullUpText: '上拉加载更多',
        });
      } else {
        that.setData({
          isPullUp: false,
          isPullDown: false,
          isPullUpText: '没有更多数据了'
        });
      }
    })
  },
  userCredit: function (e) {
    var userid = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: '../user/credit?userId=' + userid
    })
  }
})